package com.cmccgz.udacs_landlord_webapp.main.warning;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.cmccgz.baselibrary.base.BaseActivity;
import com.cmccgz.udacs_landlord_webapp.base.AppConfig;
import com.cmccgz.udacs_landlord_webapp.bean.DeviceInfoBean;
import com.cmccgz.udacs_landlord_webapp.bean.WarningDetail;
import com.cmccgz.udacs_landlord_webapp.main.warning.view.DevicesStateFragment;
import com.cmccgz.udacs_landlord_webapp.main.warning.view.WarningDealFragment;
import com.cmccgz.udacs_landlord_webapp.main.warning.view.WarningDetailFragment;
import com.cmccgz.udacs_landlord_webapp.main.warning.view.WarningEditFragment;
import com.cmccgz.udacs_landlord_webapp.main.warning.view.WarningListFragment;

public class WarningActivity extends BaseActivity{

    @Override
    protected int setLayoutId() {
        return 0;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initFragment() {
        showFragment(new WarningListFragment());
    }

    /**
     * 是否维护人员
     * @return
     */
    public boolean isAttendant(){
        boolean isAccendant = AppConfig.getAccendant(mContext);
        Log.e("inx", "isAttendant: "+ isAccendant);
        return isAccendant;
    }
    public void showWarningDetail(String w_id){
        WarningDetailFragment fragment = new WarningDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putString("w_id",w_id);
        fragment.setArguments(bundle);
        showFragment(fragment);
    }
    public void editWarning(int mode, WarningDetail detail,DeviceInfoBean device){
        WarningEditFragment fragment = new WarningEditFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("mode",mode);
        bundle.putSerializable("WarningDetail",detail);
        bundle.putSerializable("device",device);
        fragment.setArguments(bundle);
        showFragment(fragment);
    }
    public void editWarning(int mode){
        editWarning(mode,null,null);
    }
    public void editWarning(int mode,DeviceInfoBean device){
        editWarning(mode,null,device);
    }
    public void editWarning(int mode,WarningDetail detail){
        editWarning(mode,detail,null);
    }
    public void dealWarning(WarningDetail detail){
        WarningDealFragment fragment = new WarningDealFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("WarningDetail",detail);
        fragment.setArguments(bundle);
        showFragment(fragment);
    }
    public void setDevice(DeviceInfoBean device){
        WarningEditFragment warningEditFragment = (WarningEditFragment) fragmentManager.findFragmentByTag(WarningEditFragment.class.getSimpleName());
        if (warningEditFragment!=null) {
            warningEditFragment.setDevice(device);
        }else{//当为空的时候，为新建报障
            editWarning(WarningEditFragment.MODE_ADD,device);
        }
    }

    public void selectDevices(){
        DevicesStateFragment fragment = new DevicesStateFragment();
        showFragment(fragment);
    }

    public void refreshDetail(){
        WarningDetailFragment fragment = (WarningDetailFragment) fragmentManager.findFragmentByTag(WarningDetailFragment.class.getSimpleName());
        if (fragment==null) {
            return;
        }
        fragment.refreshDetail();
    }

    public void refreshWarningList(){
        WarningListFragment fragment = (WarningListFragment) fragmentManager.findFragmentByTag(WarningListFragment.class.getSimpleName());
        if (fragment==null) {
            return;
        }
        fragment.initList();
    }
    @Override
    protected void beforePermission() {

    }

    @Override
    protected int setSystemBarColorId() {
        return 0;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        WarningEditFragment warningEditFragment = (WarningEditFragment) fragmentManager.findFragmentByTag(WarningEditFragment.class.getSimpleName());
        if (warningEditFragment!=null) {
            warningEditFragment.onActivityResult(requestCode,resultCode,data);
        }
    }

    @Override
    public void onBackPressed() {
        WarningDetailFragment warningDetailFragment = (WarningDetailFragment) fragmentManager.findFragmentByTag(WarningDetailFragment.class.getSimpleName());
        if (warningDetailFragment!=null&&warningDetailFragment.isMenuShowing()) {
            warningDetailFragment.dismissMenu();
            return;
        }
        super.onBackPressed();
    }
}
